package dream.service;

import dream.mapper.ArticleMapper;
import dream.pojo.Article;
import dream.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class ArticleService{

    //1. 创建静态工厂
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public List<Article> SelectById(int id){

        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        List<Article> articles = mapper.selectById(id);

        //System.out.println(articles);
        sqlSession.close();
        return articles;

    }

    public void InsertBlog(String title, String context, int id) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        mapper.insertBlog(title, context, id);

        //提交事务，否则数据库没有数据
        sqlSession.commit();
        sqlSession.close();
    }

    public Article SelectByAid(int aid){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        Article article = new Article();

        article = mapper.selectByAid(aid);

        sqlSession.close();
        return article;
    }

    public String SelectUser(int id){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        String username = mapper.selectUser(id);

        sqlSession.close();
        return username;
    }

    public int SelectArticleCount(int id){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        int count = mapper.selectArticleCount(id);

        sqlSession.close();
        return count;
    }

    public void UpdateByAid(String title, String context, int aid){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        mapper.updateByAid(title, context, aid);
        sqlSession.commit();

        sqlSession.close();
    }

    public void UpdateReadCount(int aid){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        mapper.updateReadCount(aid);
        sqlSession.commit();

        sqlSession.close();
    }

    public void DeleteBlog(int aid){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        mapper.deleteBlog(aid);

        sqlSession.commit();
        sqlSession.close();
    }

    public List<Article> showBlogByPageServlet(int currPage, int pageSize){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        List<Article> articles = mapper.showBlogByPageServlet(currPage, pageSize);

        sqlSession.close();

        return articles;
    }

    public int SelectGood(int id, int aid){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        int count = mapper.selectGood(id, aid);

        sqlSession.close();
        return count;
    }

    public int SelectYes(int id, int aid){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        int count = mapper.selectYes(id, aid);

        sqlSession.close();

        return count;
    }

    public void AddGoodCount(int aid){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        mapper.addGoodCount(aid);

        sqlSession.commit();
        sqlSession.close();
    }

    public void InsertGood(int id, int aid){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        mapper.insertGood(id, aid);

        sqlSession.commit();
        sqlSession.close();
    }

    public void InsertYes(int id, int aid){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        mapper.insertYes(id, aid);

        sqlSession.commit();
        sqlSession.close();
    }

    public int SelectGoodCount(int aid){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        int count = mapper.selectGoodCount(aid);

        sqlSession.close();
        return count;
    }

    public List<Article> SelectByVal(String val){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        List<Article> articles = mapper.selectByVal(val);

        sqlSession.close();
        return articles;
    }

    public int[] SelectCollectById(int id){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取Mapper
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        int[] aids = mapper.selectCollectById(id);

        sqlSession.close();
        return aids;
    }
}
